package com.kly.util;


import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Component
public class HttpClient {

    @Autowired(required=false)
    private RestTemplate restTemplate;


    /**
     * 发送get请求
     */
    public String getData(String url, Object... uriVariables) {
        return getData(url, String.class, uriVariables);
    }

    /**
     * 发送get请求
     */
    public <T> T getData(String url, Class<T> responseType, Object... uriVariables) {
        return restTemplate.getForObject(url, responseType, uriVariables);
    }

    /**
     * 发送post请求
     *
     * @param url  请求URL地址
     * @param data json数据
     */
    public String postJsonData(String url, String data) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", "application/json");
        headers.add("Content-Encoding", "UTF-8");
        headers.add("Content-Type", "application/json; charset=UTF-8");
        HttpEntity<String> formEntity = new HttpEntity<>(data, headers);
        return restTemplate.postForObject(url, formEntity, String.class);
    }

    public String postJsonData(String url, Map<String, String> data) {
        String basicParams = "{\"timestamp\":\"\",\"channel\":\"1\",\"appsFlyerUID\":\"\",\"appCode\":\"sparksy\",\"applicationID\":\"com.meboo.ios\",\"appVersion\":\"1\",\"token\":\"\",\"appVersionName\":\"1.1.0\",\"appSource\":\"2\"}";
        Map<String, String> headers = new HashMap<>();
        headers.put("Accept", "application/json");
        headers.put("Content-Encoding", "UTF-8");
        headers.put("Content-Type", "application/json; charset=UTF-8");
        headers.put("basicParams", basicParams);

        return HttpUtil.postPageBody(url, headers, JSONObject.toJSONString(data));
//        return HttpUtil.postPage(url, data);
    }

    /**
     * 发送post请求
     *
     * @param url  请求URL地址
     * @param data json数据
     */
    public String postJsonData(String url, String data, Map<String, String> headerMap) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", "application/json");
        headers.add("Content-Encoding", "UTF-8");
        headers.add("Content-Type", "application/json; charset=UTF-8");
        if (!CollectionUtils.isEmpty(headerMap)) {
            headerMap.forEach(headers::add);
        }
        HttpEntity<String> formEntity = new HttpEntity<>(data, headers);
        return restTemplate.postForObject(url, formEntity, String.class);
    }

    /**
     * post 提交表单数据
     * application/x-www-form-urlencoded
     */
    public String postFormData(String url, Map<String, String> params, Object... uriVariables) {
        return postFormData(url, params, String.class, uriVariables);
    }

    /**
     * post 提交表单数据
     * application/x-www-form-urlencoded
     */
    public <T> T postFormData(String url, Map<String, String> params, Class<T> responseType, Object... uriVariables) {
        return postFormData(url, params, responseType, MediaType.APPLICATION_FORM_URLENCODED, uriVariables);
    }

    /**
     * post 提交表单数据
     * multipart/form-data
     */
    public <T> T postMultipartFormData(String url, Map<String, String> params, Class<T> responseType, Object... uriVariables) {
        return postFormData(url, params, responseType, MediaType.MULTIPART_FORM_DATA, uriVariables);
    }

    /**
     * post 提交表单数据
     */
    private <T> T postFormData(String url, Map<String, String> params, Class<T> responseType, MediaType mediaType, Object... uriVariables) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(mediaType);
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.setAll(params);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        return restTemplate.postForObject(url, request, responseType, uriVariables);
    }
}
